package com.map.app;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.map.home.LoadData;
import com.map.home.SecUsersHome;
import com.map.manager.ConfigManager;
import com.map.util.Constants;
import com.map.util.StringUtil;

class Login extends JFrame implements ActionListener, KeyListener {

	private static final long serialVersionUID = 1L;
	private static Login login = null;
	
	JButton SUBMIT;
	JPanel panel;
	JLabel label1, label2, labelMsg, labelMsg2;
	final JTextField text1, text2;

	Login() {
		labelMsg  = new JLabel();
		labelMsg2 = new JLabel();
		
		label1 = new JLabel();
		label1.setText("   Login Id:");
		text1 = new JTextField(15);

		label2 = new JLabel();
		label2.setText("   Password:");
		text2 = new JPasswordField(15);
		
		//text1.setText("mbbadmin");
		//text2.setText("123456");
		
		SUBMIT = new JButton("SUBMIT");

		panel = new JPanel(new GridLayout(4, 1));
		panel.add(labelMsg);
		panel.add(labelMsg2);
		panel.add(label1);
		panel.add(text1);
		panel.add(label2);
		panel.add(text2);
		panel.add(SUBMIT);
		add(panel, BorderLayout.CENTER);
		SUBMIT.addActionListener(this);
		text2.addKeyListener(this);
		setTitle("Autoscript Tool - Login Form");
		setLocationRelativeTo(null); // *** this will center your app ***
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setSize(350, 150);
	}

	public void showMessage(String msg) {
		labelMsg.setText(msg);
	}

	public void actionPerformed(ActionEvent ae) {
		submit();
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode()==KeyEvent.VK_ENTER){
			submit();
		}
	}	
	private void submit() {
		String loginId = text1.getText();
		String pwd     = text2.getText();
		try {
			// check database connection is ok or not?
			String res = LoadData.getInstance().checkConnection();
			if (StringUtil.isEmpty(res)) {
				if (!StringUtil.isEmpty(loginId) && !StringUtil.isEmpty(pwd)) {
					if (SecUsersHome.getInstance().login(Constants.MER_NAME, loginId, pwd) ) {
	
						// check permission
						if (SecUsersHome.getInstance().checkPermission(loginId, ConfigManager.getInstance().get("mas.module.code"))) {
							AutoScript.getInstance().start(loginId);
							login.setVisible(false);
						} else {
							System.out.println("do not have permission to access");
							JOptionPane.showMessageDialog(this, "no access right error",
									"Error", JOptionPane.ERROR_MESSAGE);
						}
		
					} else {
						System.out.println("login error");
						JOptionPane.showMessageDialog(this, "login error",
								"Error", JOptionPane.ERROR_MESSAGE);
					}
				} else {
					// need to enter loginId/password
					JOptionPane.showMessageDialog(getContentPane(), "You need to input the Login Id and Password");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String arg[]) {
		try {

			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}

			/*
			 * Create and display the form
			 */
			java.awt.EventQueue.invokeLater(new Runnable() {

				public void run() {
					login = new Login();
					login.setVisible(true);
					
					// check database connection is ok or not?
					String res = LoadData.getInstance().checkConnection();
					if (StringUtil.isEmpty(res)) {
						login.showMessage("  Welcome");
					} else {
						login.showMessage("  DB connection error");
					}
					
				}
			});

		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {}

	@Override
	public void keyReleased(KeyEvent e) {}
}